<template>
  <div class="content">
    <el-card class="content-card">
      <div slot="header" class="content-card-header">
        <span>设置我的资料</span>
      </div>
      <div class="content-card-body">
        <el-form ref="ruleForm" label-width="110px" :model="form" :rules="bindFormRules">
          <el-form-item label="网站名称" prop="websiteName">
            <el-input v-model="form.websiteName" placeholder="请输入网站名称" clearable />
          </el-form-item>
          <el-form-item label="网站域名" prop="websiteDomain">
            <el-input v-model="form.websiteDomain" placeholder="请输入网站域名" clearable />
          </el-form-item>
          <el-form-item label="缓存时间(分)" prop="cacheTime">
            <el-input v-model.number="form.cacheTime" placeholder="请输入缓存时间" type="number" />
          </el-form-item>
          <el-form-item label="首页标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入首页标题..." clearable />
          </el-form-item>
          <el-form-item label="版权信息" prop="copyrightInfo">
            <el-input v-model="form.copyrightInfo" :rows="4" type="textarea" placeholder="请输入版权信息..." />
          </el-form-item>
          <el-form-item label="页尾信息" prop="footerDescription">
            <el-input v-model="form.footerDescription" :rows="4" type="textarea" placeholder="请输入页尾信息..." />
          </el-form-item>
          <el-form-item label="上次修改时间">
            <el-tag type="info">{{ form.updateTime }}</el-tag>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="medium" @click="handleUpdate">确认修改</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getOne, update } from '@/api/config'

export default {
  data() {
    var validateCacheTime = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请正确填写缓存时间'))
      } else {
        if (value < 0) {
          callback(new Error('缓存时间必须大于0'))
        }
        callback()
      }
    }
    return {
      bindFormRules: {
        websiteName: [{ required: true, message: '网站名称不能为空', trigger: 'change' }],
        websiteDomain: [{ required: true, message: '网站域名不能为空', trigger: 'change' }],
        cacheTime: [
          { required: true, message: '缓存时间不能为空', trigger: 'change' },
          { validator: validateCacheTime, trigger: 'change' }
        ],
        title: [{ required: true, message: '首页标题不能为空', trigger: 'change' }],
        copyrightInfo: [{ required: true, message: '版权信息不能为空', trigger: 'change' }],
        footerDescription: [{ required: true, message: '页尾不能为空', trigger: 'change' }]
      },
      form: {
        websiteName: '',
        websiteDomain: '',
        cacheTime: 0,
        title: '',
        copyrightInfo: '',
        footerDescription: ''
      }
    }
  },
  created() {
    this.getOne()
  },
  methods: {
    getOne() {
      getOne(0).then(response => {
        if (response.code === 200) {
          this.form = response.data
        } else {
          this.$message({
            type: 'error',
            message: response.message
          })
        }
      })
    },
    // 更新操作
    handleUpdate() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          update(this.form).then(response => {
            if (response.code === 200) {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
            } else {
              this.$message({
                type: 'error',
                message: response.message
              })
            }
          })
        } else {
          this.$message({
            type: 'error',
            message: '输入有误，请重新检查'
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .content {
    padding: 20px;
  }

  .content-card-header:before,
  .content-card-header:after {
    display: table;
    content: "";
  }
  .content-card-header:after {
    clear: both
  }

  .content-card-body {
    width: 460px;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
